package com.ykw.parking.controller;

import com.ykw.parking.mapper.OrderMapper;
import com.ykw.parking.mapper.OwnerMapper;
import com.ykw.parking.mapper.UserMapper;
import com.ykw.parking.pojo1.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class ServicesController {
    @Autowired
    UserMapper userMapper;

    @Autowired
    OwnerMapper ownerMapper;
    @Autowired
    OrderMapper orderMapper;
    @RequestMapping("/services")
    public String index(HttpSession session,Model model){
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        UserOwner userOwner = ownerMapper.queryOwnerByUid(user.getId());
        if(userOwner!=null){
            if(userOwner.getState()!=1){
                model.addAttribute("msg","业主身份待审核");
            }else{
                model.addAttribute("msg","1");
                model.addAttribute("userOwner",userOwner);
            }
        }else{
            model.addAttribute("msg","你不是业主");
        }
        return "services";
    }

    @RequestMapping(value = "/addParkingLot",method = RequestMethod.POST)
    @ResponseBody
    public String addParkingLot(String city,String address,String number,
                                String price,String open,String explain,HttpSession session){
        System.out.println("number="+number+"===price="+price+"------open="+open+"---explain"+explain);
        String  username = (String)session.getAttribute("username");
        User user = userMapper.queryUserbyname(username);
        UserOwner userOwner = ownerMapper.queryOwnerByUid(user.getId());
        Parking parkingOne = ownerMapper.queryParkingOneByAddress(userOwner.getAddress());
        Integer isifi;
        Integer isifo;
        if(open.equals("否")){
            isifi=0;
            isifo=0;
        }else if (open.equals("是")){
            isifi=1;
            isifo=1;
        }else{
            return "添加失败";
        }
        System.out.println("isifi"+isifi);
        System.out.println("isifo"+isifo);
        ParkingLot parkingLot = ownerMapper.queryParkingLotByPidByNum(String.valueOf(parkingOne.getId()), number);
        if(parkingLot!=null){
            System.out.println("车位已经存在");
            return "该车位已存在";
        }
        System.out.println("111");
        int i = ownerMapper.AddParkingLot(new ParkingLot(null, String.valueOf(parkingOne.getId()), isifi, isifo, number, Double.valueOf(price),
                explain, 0));
        ownerMapper.updatePakingNum(parkingOne.getNumber()+1,parkingOne.getId());
        return "添加成功";

    }
}
